<?
    /* -*- Mode: PHP5; tab-width: 4; indent-tabs-mode: nil; basic-offset: 4 -*- */

    /*********************************************************
    *            --== InArch ==--
    *
    * @author Kulikov Alexey <a.kulikov@gmail.com>
    * @version n/a
    * @copyright essentialmind.com 2005
    *
    *********************************************************/
    
    /***
    * Class adminUsers
    ****
    * This class represents the users part of a solution
    ****
    * @package adminModules
    * @author A Kulikov <a.kulikov@gmail.com>
    * @version n/a
    * @copyright essentialmind.com 2005
    ***/
    class adminSuppliers extends adminModule{

        /**
         * Process user inputs
         * 
         * @return void 
         **/
        public function process(){
            if($_POST['addNewSupplier']){
                $this->addNewSupplier();
            }
            
            $this->smarty->assign('suppliers',$this->db->getAll("SELECT * FROM shop_suppliers ORDER BY name"));
            $this->smarty->assign('ntypes',$this->db->getAssoc("SELECT id, name FROM shop_notifications ORDER BY name"));
        }
        
        
        protected function addNewSupplier(){
            $errors = array();
            
            //email
            if(!eregi("^[A-Za-z0-9_\.\-]+@[A-Za-z0-9_\.\-]+\.[A-Za-z0-9_\-][A-Za-z0-9_\-]+$", $_POST['mail'])){
                $errors['mail'] = true;
            }
            
            if(!$_POST['name']){
                $errors['name'] = true;
            }
            
            
            if(empty($errors)){
                if((int)$_POST['notification'] == 0){
                    $_POST['notification'] = 'NULL';
                }else{
                    $_POST['notification'] = (int)$_POST['notification'];
                }
                
                //lets try some magic
                try{
                    //update?
                    if($_POST['id']){
                    
                        $this->log->recordAction("Updating Supplier -- \"".$_POST['name']."\"");
        
                        //start transaction
                        $this->db->StartTrans();
                        $this->db->Execute("UPDATE shop_suppliers SET
                                                        name    = ".$this->db->qstr($_POST['name']).",
                                                        adr1    = ".$this->db->qstr($_POST['adr1']).",
                                                        adr2    = ".$this->db->qstr($_POST['adr2']).",
                                                        city    = ".$this->db->qstr($_POST['city']).",
                                                        country = ".$this->db->qstr($_POST['country']).",
                                                        zip     = ".$this->db->qstr($_POST['zip']).",
                                                        tel     = ".$this->db->qstr($_POST['tel']).",
                                                        fax     = ".$this->db->qstr($_POST['fax']).",
                                                        mail    = ".$this->db->qstr($_POST['mail']).",
                                                        notes   = ".$this->db->qstr($_POST['notes']).",
                                                        
                                                        p1name  = ".$this->db->qstr($_POST['1pname']).",
                                                        p1pos   = ".$this->db->qstr($_POST['1ppos']).",
                                                        p1notes = ".$this->db->qstr($_POST['1pnotes']).",
                                                        
                                                        p2name  = ".$this->db->qstr($_POST['2pname']).",
                                                        p2pos   = ".$this->db->qstr($_POST['2ppos']).",
                                                        p2notes = ".$this->db->qstr($_POST['2pnotes']).",
                                                        
                                                        p3name  = ".$this->db->qstr($_POST['3pname']).",
                                                        p3pos   = ".$this->db->qstr($_POST['3ppos']).",
                                                        p3notes = ".$this->db->qstr($_POST['3pnotes']).",
                                                        
                                                        notification =    ".$_POST['notification']."
                                                    WHERE id = ".$_POST['id']."
                                                      ");
                                                        
                        //end transaction
                        $this->db->CompleteTrans();
                        
                        //log action
                        $this->log->recordAction("Successfully updated supplier -- \"".$_POST['name']."\"");

                    //add
                    }else{
                        $this->log->recordAction("Adding a new Supplier -- \"".$_POST['name']."\"");
        
                        //start transaction
                        $this->db->StartTrans();
                        $this->db->Execute("INSERT INTO shop_suppliers(name, adr1, adr2, city, country, zip, tel, fax, mail, notification,notes,p1name,p1pos,p1notes,p2name,p2pos,p2notes,p3name,p3pos,p3notes)
                                                    VALUES(
                                                            ".$this->db->qstr($_POST['name']).",
                                                            ".$this->db->qstr($_POST['adr1']).",
                                                            ".$this->db->qstr($_POST['adr2']).",
                                                            ".$this->db->qstr($_POST['city']).",
                                                            ".$this->db->qstr($_POST['country']).",
                                                            ".$this->db->qstr($_POST['zip']).",
                                                            ".$this->db->qstr($_POST['tel']).",
                                                            ".$this->db->qstr($_POST['fax']).",
                                                            ".$this->db->qstr($_POST['mail']).",
                                                            
                                                            ".$_POST['notification'].",
                                                            
                                                            ".$this->db->qstr($_POST['notes']).",
                                                            
                                                            ".$this->db->qstr($_POST['1pname']).",
                                                            ".$this->db->qstr($_POST['1ppos']).",
                                                            ".$this->db->qstr($_POST['1pnotes']).",
                                                            
                                                            ".$this->db->qstr($_POST['2pname']).",
                                                            ".$this->db->qstr($_POST['2ppos']).",
                                                            ".$this->db->qstr($_POST['2pnotes']).",
                                                            
                                                            ".$this->db->qstr($_POST['3pname']).",
                                                            ".$this->db->qstr($_POST['3ppos']).",
                                                            ".$this->db->qstr($_POST['3pnotes'])."
                                                            
                                                            
                                                        )");
                                                        
                        //end transaction
                        $this->db->CompleteTrans();
                        
                        //log action
                        $this->log->recordAction("Successfully added supplier -- \"".$_POST['name']."\"");
                    }
                    
                    //reset POST
                    unset($_POST);
                    
                    //redirect
                    header('Location: '.$GLOBALS['_CONFIG']['admin_root'].$_GET['view']  .'/?confirm=true');
                    exit;
                    
                }catch(Exception $e){
                    
                    
                    //log error
                    $this->log->recordError($e->getMessage());
                    
                    //redirect browser to tech error page
                    //has to be implemented
                }
            }else{
            
                $this->smarty->assign('groupErrors',$errors);
            
            }
        }
        
        
        /**
         * Set action template to controlPanel.html
         * 
         * @return void
         **/
        protected function setTemplate(){
            $this->setActionTemplate('suppliers.html');
        }
    }
?>